package main

import (
	"fmt"
	httpserver "modbus-dtu-plugin/http_server"
	m "modbus-dtu-plugin/modbus"
	"modbus-dtu-plugin/zaplog"

	mqtt "modbus-dtu-plugin/mqtt"
	mqtt_sub "modbus-dtu-plugin/mqtt_sub"

	"github.com/spf13/viper"
)

func main() {
	zaplog.Init()
	fmt.Println("**************************************************")
	zaplog.Logger.Info("modbus-dtu-plugin start")
	// 当程序退出时，需要刷新缓存中的日志
	defer zaplog.Sync()
	// 启动http服务
	go httpserver.HttpServer()

	mqtt.Init()
	mqtt_sub.Init()

	// 获取平台配置并连接设备
	rtuFlag := viper.GetInt("protocol.type1_flag")
	if rtuFlag == 1 {
		protocol := viper.GetString("protocol.type1")
		m.StartModbus(protocol)
		fmt.Println("StartRTU")
	}
	tcpFlag := viper.GetInt("protocol.type2_flag")
	if tcpFlag == 1 {
		protocol := viper.GetString("protocol.type2")
		m.StartModbus(protocol)
		fmt.Println("StartTCP")
	}

	//等待
	select {}

}
